﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chloe;
using lingopen.Application.Jabil.Models.TTS;
using lingopen.Entity.CNC;
using lingopen.Entity.EPC;

namespace lingopen.Application.Jabil.Implements
{
    public class EPCAppService : AdminAppService, IEPCAppService
    {
        public bool Check_Flow(string prod_code, string fix_code, string type)
        {
            var query = this.DbContext.Query<Pass_Flow>().FilterDeleted()
                .Where(p => p.Station_Type == type).FirstOrDefault();
            if (query == null) return false;
            if (query.Flow_Order - 1 >= 0)
            {
                var old_type = this.DbContext.Query<Pass_Flow>().FilterDeleted()
                    .Where(p => p.Flow_Order == query.Flow_Order - 1).FirstOrDefault(); //前到工序的 type
                if (old_type == null) return false;
                if (old_type.Station_Type.ToLower().StartsWith("link"))
                {
                    var exist = this.DbContext.Query<EPC_Banding>().FilterDeleted()
                       .Where(p => p.Station_Type == old_type.Station_Type && p.Prod_Code == prod_code && p.Tool_Code == fix_code);
                    if (exist.Any()) return true;
                    else return false;
                }
                else
                {
                    var exist = this.DbContext.Query<CNC_Check>().FilterDeleted()
                     .Where(p => p.Station_Type == old_type.Station_Type && p.Prod_Code == prod_code && p.Tool_Code == fix_code);
                    if (exist.Any()) return true;
                    else return false;
                }
            }
            else return true;

        }
        public bool Check_Repeate(string prod_Code, string fix_code, string type)
        {
            var query = this.DbContext.Query<CNC_Check>().FilterDeleted()
               .Where(p => p.Station_Type == type && p.Prod_Code == prod_Code && p.Tool_Code == fix_code);
            if (query.Any()) return false;
            else return true;
        }
        public bool Pass_Repeate(string prod_Code, string fix_code, string type)
        {
            var query = this.DbContext.Query<CNC_Transfer>().FilterDeleted()
               .Where(p => p.Station_Type == type && p.Prod_Code == prod_Code && p.Tool_Code == fix_code);
            if (query.Any()) return false;
            else return true;

        }
        public List<ProdItem> Query_TTS_Prods(string keyword)
        {
            string sql = $@"
  select Station_Type, Prod_Code,Tool_Code,'' Machine_Code,'' Prod_Line ,null Start_Time,null End_Time,CreationTime from [epc_banding]  where IsDeleted=0  and Station_Type is not null and Prod_Code='{keyword}'
  union all 
  select Station_Type, Prod_Code,Tool_Code,  Machine_Code,  Prod_Line , Start_Time,  End_Time,CreationTime from cnc_transfer  where IsDeleted=0 and Station_Type is not null and Prod_Code='{keyword}'
  order by CreationTime
";
            var q = this.DbContext.SqlQuery<ProdItem>(sql, null);
            return q.ToList<ProdItem>();
        }
        public PagedData<CNC_Transfer> Query_CNC_Transfers(Pagination pagination, string keyword, string type)
        {
            var q = this.DbContext.Query<CNC_Transfer>().FilterDeleted()
                .Where(p => p.Station_Type == type)
                .WhereIfNotNullOrEmpty(keyword, a => a.Prod_Code.Contains(keyword) || a.Tool_Code.Contains(keyword) || a.Machine_Code.Contains(keyword))
                .OrderByDesc(a => a.CreationTime);
            PagedData<CNC_Transfer> ret = q.TakePageData(pagination);
            return ret;
        }

        public PagedData<EPC_Banding> Query_EPC_Bandings(Pagination pagination, string keyword, string type)
        {
            var q = this.DbContext.Query<EPC_Banding>().FilterDeleted()
                .Where(p => p.Station_Type == type)
                .WhereIfNotNullOrEmpty(keyword, a => a.Prod_Code.Contains(keyword) || a.Tool_Code.Contains(keyword))
                .OrderByDesc(a => a.CreationTime);
            PagedData<EPC_Banding> ret = q.TakePageData(pagination);
            return ret;
        }
        public bool Check_Link(string tool_code, string prod_code, string type)
        {
            var q = this.DbContext.Query<EPC_Banding>().FilterDeleted()
               .Where(a => a.Prod_Code == prod_code && a.Tool_Code == tool_code && a.Station_Type == type);
            return q.Any();
        }

        public EPC_Banding Banding(Add_EPC_Banding input, string type)
        {
            input.Station_Type = type;
            if (Check_Link(input.Tool_Code, input.Prod_Code, type))
            {
                DbContext.Update<EPC_Banding>(a => a.Prod_Code == input.Prod_Code && a.Station_Type == type, a => new EPC_Banding() { IsDeleted = true });
            }
            var add = DbContext.InsertFromDto<EPC_Banding, Add_EPC_Banding>(input);
            return add;
        }
        public EPC_Banding Get_Banding(string tool_code, string type)
        {
            var q = this.DbContext.Query<EPC_Banding>().FilterDeleted()
              .Where(a => a.Tool_Code == tool_code && a.Station_Type == type)
              .OrderByDesc(p => p.CreationTime);
            return q.FirstOrDefault();
        }
    }
}
